package com.jxt.myjxtmanager.db.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;


import com.jxt.myjxtmanager.db.DaoSession;
import com.jxt.myjxtmanager.db.Student;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by qm on 2017/3/7 0007.
 */
public class StudentDao extends AbstractDao<Student, Long> {
    public static final String TABLENAME = "STUDENT";

    /**
     * Properties of entity Message_task.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property StudentNum = new Property(1, String.class, "studentNum", false, "STUDENTNUM");
        public final static Property StudentName = new Property(2, String.class, "studentName", false, "STUDENTNAME");
        public final static Property Sex = new Property(3, String.class, "sex", false, "SEX");
        public final static Property Birthday = new Property(4, String.class, "birthday", false, "BIRTHDAY");
        public final static Property Address = new Property(5, String.class, "address", false, "ADDRESS");
        public final static Property MyClass = new Property(6, String.class, "myClass", false, "MYCLASS");
        public final static Property ParentName = new Property(7, String.class, "parentName", false, "PARENTNAME");
        public final static Property ParentPhone = new Property(8, String.class, "parentPhone", false, "PARENTPHONE");
        public final static Property UrgentPhone = new Property(9, String.class, "urgentPhone", false, "URGENTPHONE");


        public final static Property IntoSchoolTimeAM = new Property(10, String.class, "intoSchoolTimeAM", false, "INTOSCHOOLTIMEAM");
        public final static Property OutSchoolTimeAM = new Property(11, String.class, "outSchoolTimeAM", false, "OUTSCHOOLTIMEAM");
        public final static Property IntoSchoolTimePM = new Property(12, String.class, "intoSchoolTimePM", false, "INTOSCHOOLTIMEPM");
        public final static Property OutSchoolTimePM = new Property(13, String.class, "outSchoolTimePM", false, "OUTSCHOOLTIMEPM");
        public final static Property TodayCheckWork = new Property(14, String.class, "todayCheckWork", false, "TODAYCHECKWORK");
        public final static Property IsLate = new Property(15, String.class, "isLate", false, "ISLATE");
    }

    public StudentDao(DaoConfig config) {
        super(config);
    }

    public StudentDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "'STUDENT' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'STUDENTNUM' TEXT NOT NULL ," + // 1: send_time
                "'STUDENTNAME' TEXT," + // 3: content
                "'SEX' TEXT," + // 5: title
                "'BIRTHDAY' TEXT," + // 5: title
                "'ADDRESS' TEXT," + // 5: title
                "'MYCLASS' TEXT," + // 5: title
                "'PARENTNAME' TEXT," + // 5: title
                "'PARENTPHONE' TEXT," + // 5: title
                "'URGENTPHONE' TEXT," + // 5: title
                "'INTOSCHOOLTIMEAM' TEXT," + // 5: title
                "'OUTSCHOOLTIMEAM' TEXT," + // 5: title
                "'INTOSCHOOLTIMEPM' TEXT," + // 5: title
                "'OUTSCHOOLTIMEPM' TEXT," + // 5: title
                "'TODAYCHECKWORK' TEXT," + // 5: title
                "'ISLATE' TEXT);"); // 7: un_read_message
    }

    /**
     * Drops the underlying database table.
     */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'STUDENT'";
        db.execSQL(sql);
    }

    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, Student entity) {
        stmt.clearBindings();

        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getStudentNum());
        stmt.bindString(3, entity.getStudentName());

        String sex = entity.getSex();
        if (sex != null) {
            stmt.bindString(4, sex);
        }

        stmt.bindString(5, entity.getBirthday());

        String address = entity.getAddress();
        if (address != null) {
            stmt.bindString(6, address);
        }
        String myClass = entity.getMyClass();
        if (myClass != null) {
            stmt.bindString(7, myClass);
        }

        String parentName = entity.getParentName();
        if (parentName != null) {
            stmt.bindString(8, parentName);
        }
        String parentPhone = entity.getParentPhone();
        if (parentPhone != null) {
            stmt.bindString(9, parentPhone);
        }
        String urgentPhone = entity.getUrgentPhone();
        if (urgentPhone != null) {
            stmt.bindString(10, urgentPhone);
        }

        String intoSchoolTimeAM = entity.getIntoSchoolTimeAM();
        if (intoSchoolTimeAM != null) {
            stmt.bindString(11, intoSchoolTimeAM);
        }
        String outSchoolTimeAM = entity.getOutSchoolTimeAM();
        if (outSchoolTimeAM != null) {
            stmt.bindString(12, outSchoolTimeAM);
        }
        String intoSchoolTimePM = entity.getIntoSchoolTimePM();
        if (intoSchoolTimePM != null) {
            stmt.bindString(13, intoSchoolTimePM);
        }
        String outSchoolTimePM = entity.getOutSchoolTimePM();
        if (outSchoolTimePM != null) {
            stmt.bindString(14, outSchoolTimePM);
        }
        String todayCheckWork = entity.getTodayCheckWork();
        if (todayCheckWork != null) {
            stmt.bindString(15, todayCheckWork);
        }
        String isLate = entity.getIsLate();
        if (isLate != null) {
            stmt.bindString(16, isLate);
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    /**
     * @inheritdoc
     */
    @Override
    public Student readEntity(Cursor cursor, int offset) {
        Student entity = new Student(
                cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
                cursor.getString(offset + 1), // send_time
                cursor.getString(offset + 2), // status
                cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // content
                cursor.getString(offset + 4), // msg_type
                cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // title
                cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // title
                cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // un_read_message
                cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // un_read_message
                cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // un_read_message
                cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10), // un_read_message
                cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11), // un_read_message
                cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12), // un_read_message
                cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // un_read_message
                cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // un_read_message
                cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15) // un_read_message
        );
        return entity;
    }

    /**
     * @inheritdoc
     */
    @Override
    public void readEntity(Cursor cursor, Student entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setStudentNum(cursor.getString(offset + 1));
        entity.setStudentName(cursor.getString(offset + 2));
        entity.setSex(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setBirthday(cursor.getString(offset + 4));
        entity.setAddress(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setMyClass(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setParentName(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setParentPhone(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setUrgentPhone(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setIntoSchoolTimeAM(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
        entity.setOutSchoolTimeAM(cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11));
        entity.setIntoSchoolTimePM(cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12));
        entity.setOutSchoolTimePM(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setTodayCheckWork(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
        entity.setIsLate(cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15));
    }

    /**
     * @inheritdoc
     */
    @Override
    protected Long updateKeyAfterInsert(Student entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long getKey(Student entity) {
        if (entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}
